package cn.monkey.rivergrab;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class PreviewImage extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preview_image);
		Gallery mGallery = (Gallery)findViewById(R.id.gallery);
		
		
		
		mGallery.setAdapter(new ImageAdapter(this));
	}
	private String[] getImage(){
		String[] files = null;
		AssetManager assertManager = getAssets();
		try {
			files = assertManager.list("");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return files;
	}
	private class ImageAdapter extends BaseAdapter{
		private Context mContext;
		private String[] files = null;
		public ImageAdapter(Context context){
			this.mContext = context;
			files = getImage();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return files.length;
		}
	
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}
	
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
	
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView iv = new ImageView(mContext);
			iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
			try {
				InputStream in = getAssets().open(files[position]);
				Bitmap bitmap = BitmapFactory.decodeStream(in);
				iv.setImageBitmap(bitmap);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//Bitmap bitmap = BitmapFactory.decodeFile(files[position], options);
			
			return iv;
		}
	}
}
